// SSMonitorItems.cpp: implementation of the CSSMonitorItems class.
//
//////////////////////////////////////////////////////////////////////

#include "./SSMonitorItems.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSSMonitorItems CSSMonitorItems::s_Instance;

CSSMonitorItems& CSSMonitorItems::Instance()
{
	return s_Instance;
}

CSSMonitorItems::CSSMonitorItems()
{

}

CSSMonitorItems::~CSSMonitorItems()
{
	m_mtxMap.Lock();
	MAP_ITER_STR_TO_STATITEM iter = m_mapStatic.begin();
	while ( iter != m_mapStatic.end() )
	{
		delete iter->second;
		m_mapStatic.erase( iter );
		iter = m_mapStatic.begin();
	}
	m_mtxMap.Unlock();
}

void CSSMonitorItems::Reset()
{
	m_mtxMap.Lock();
	MAP_ITER_STR_TO_STATITEM iter = m_mapStatic.begin();
	while ( iter != m_mapStatic.end() )
	{
		iter->second->Reset();
		++iter;
	}
	m_mtxMap.Unlock();
}

void CSSMonitorItems::IncCount( const string& strItemName )
{
	ModifyCount( strItemName, TRUE );
}

void CSSMonitorItems::DecCount( const string& strItemName )
{
	ModifyCount( strItemName, FALSE );
}

void CSSMonitorItems::ModifyCount( const string& strItemName, Bool bIsAdd )
{
	m_mtxMap.Lock();
	PStatItem pItem = NULL;
	MAP_ITER_STR_TO_STATITEM iter = m_mapStatic.find( strItemName );
	if ( iter == m_mapStatic.end() )
	{
		pItem = new StatItem();
		m_mapStatic[strItemName] = pItem;
	}
	else
	{
		pItem = iter->second;
	}
	if ( bIsAdd )
	{
		++(pItem->uTotal);
		++(pItem->uCurrCount);
	}
	else
	{
		--(pItem->uCurrCount);
	}
	m_mtxMap.Unlock();
}

UInt32 CSSMonitorItems::GetCount( const string& strItemName )
{
	UInt32 iResult = -1;
	m_mtxMap.Lock();
	MAP_ITER_STR_TO_STATITEM iter = m_mapStatic.find( strItemName );
	if ( iter != m_mapStatic.end() )
	{
		iResult = iter->second->uCurrCount;
	}
	m_mtxMap.Unlock();
	return iResult;
}

string CSSMonitorItems::AdjustItemNameToFitLen( const string& strItemName )
{
	string strResult = strItemName;
	string strTitle( STAT_OUTPUT_TITLE );
	size_t iItemNameLen = strTitle.find( '|' );
	if ( strResult.length() < iItemNameLen )
	{
		for( ; strResult.length() < iItemNameLen; strResult += ' ' );
	}
	else if ( strResult.length() > iItemNameLen )
	{
		strResult = strResult.substr( 0, iItemNameLen );
	}
	return strResult;
}

void CSSMonitorItems::InitEnumerator()
{
	m_mtxMap.Lock();
	m_iterTrace = m_mapStatic.begin();
}

void CSSMonitorItems::ReleaseEnumerator()
{
	m_mtxMap.Unlock();
}

Bool CSSMonitorItems::EnumStatItem( string& strItemName, UInt32& uCurrCount, UInt64& uTotal )
{
	Bool bExist = FALSE;
	MAP_ITER_STR_TO_STATITEM iter = m_iterTrace;
	if ( iter != m_mapStatic.end() )
	{
		strItemName = AdjustItemNameToFitLen(iter->first);
		uCurrCount = iter->second->uCurrCount;
		uTotal = iter->second->uTotal;
		m_iterTrace++;
		bExist = TRUE;
	}	
	return bExist;
}